//
//  UserViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserViewController.h"
#import "LoginViewController.h"
#import "MyMovieViewController.h"
#import "MyActivityViewController.h"
#import "SDImageCache.h"
#import "MBProgressHUD.h"
@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, assign)BOOL isSelected;

@property (nonatomic, retain)MBProgressHUD *hud;


@end

@implementation UserViewController

- (void)dealloc
{
    [_hud release];
    [_button release];
    [_myTableView release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"isLogin"];
//    NSLog(@"string == %@",string);
    if ([@"0"isEqualToString:string ]) {
//        NSLog(@"登陆状态");
        [self.button setTitle:@"登陆" forState:UIControlStateNormal];
    }else{
//        NSLog(@"注销状态");
        [self.button setTitle:@"注销" forState:UIControlStateNormal];

        
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    //设置标题
    self.navigationItem.title = @"我的";
    
    //创建按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, 40, 40);
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    
    //设置右侧按钮(登陆)
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.button];
    
    //改变按钮字体颜色
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    
    
    
    
}

#pragma mark -- 右侧按钮方法(登陆)

-(void)buttonAction:(UIButton *)but
{
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    // 判断登陆状态
    if (![@"1" isEqualToString:isLogin]) {
        
        // 如果未登录
       LoginViewController *logVC = [[LoginViewController alloc] init];
        
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:logVC];
        
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
        
        [logVC release];
        [naVC release];
        
        
    }else {
        
        // 已登陆
        UIAlertController *alertV = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
//            NSLog(@"11111");
            
            //注销账号
            [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"isLogin"];
            
            [self.button setTitle:@"登陆" forState:UIControlStateNormal];
            
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            return;
            
        }];
        
        [alertV addAction:action1];
        [alertV addAction:action2];
        
        [self presentViewController:alertV animated:YES completion:^{
            
        }];
        
        
    }
    

    
}


#pragma mark -- 设置区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

#pragma mark -- 设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark -- 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *useCell = @"user";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:useCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:useCell];
    }if (indexPath.section == 0) {
        cell.textLabel.text = @"我的活动";
    }else if (indexPath.section == 1){
        cell.textLabel.text = @"我的电影";
    }else{
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;
}

#pragma mark -- 触摸时候触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    // 判断登陆状态
    if ([@"1" isEqualToString:isLogin]){
        if (indexPath.section == 0) {
            MyActivityViewController *actVC = [[MyActivityViewController alloc]init];
            [self.navigationController pushViewController:actVC animated:YES];
        }
        if (indexPath.section == 1) {
            MyMovieViewController *movieVC = [[MyMovieViewController alloc]init];
            [self.navigationController pushViewController:movieVC animated:YES];
            
        }
        
    }else {
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *logNaVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        
        [self presentViewController:logNaVC animated:YES completion:^{
            
            
        }];

    }
    
    
    NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    NSString *string = [NSString stringWithFormat:@"缓存大小为%.2fMB", cacheSize];
    
    
    if (indexPath.section == 2) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleActionSheet];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];

        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"清除缓存" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            // 清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
            // hud单例
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"清除缓存成功";
            self.hud.mode = MBProgressHUDModeText;
            [self.hud hide:YES afterDelay:1];
            
        }];
        // 创建提示框按钮
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
        [alertC addAction:action1];

    }
        
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
